(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.apCommunity.updateVerifyController', updateVerifyController);
	updateVerifyController.$inject = [ '$scope', '$filter', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService', 'apCommunityService' ];
	function updateVerifyController($scope, $filter, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, apCommunityService) {
		var ctrl = this, apCommunityInfo = $scope.$stateParams.params, errorMessage = {};
		//ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.apCommunity = {};
		
		ctrl.init = function() {
			var params = {
					taskId : apCommunityInfo.taskId
			};
			// init acct
			apCommunityService.initProfAcct(layoutConfig, $scope, ctrl, true);
			// init pastdue
			apCommunityService.initProfPastdue(layoutConfig, $scope, ctrl, true);
			// init fee
			apCommunityService.initProfFee(layoutConfig, $scope, ctrl, true);
			// init doc
			apCommunityService.initEventDoccfg(layoutConfig, $scope, ctrl, true);
			// init Settlement Instruction
			apCommunityService.initProfSetlinst(layoutConfig, $scope, ctrl, true);
			// init apmember
			apCommunityService.initApMember(layoutConfig, $scope, ctrl, true);
			// init cust prop define
			apCommunityService.initCustProp(layoutConfig, $scope, ctrl, true);
			//
			apCommunityService.initGeneral($scope, ctrl, true).then(function() {
				authService.updateVerify(params).then(function(response) {
					var initApCommunity = response.data.model.newModel;
					ctrl.action = "updateVerify";
					ctrl.apCommunityOld = response.data.model.oldModel || {};
					apCommunityService.initLayoutConfigAndModel($scope, ctrl, initApCommunity, ctrl.action, false).then(function() {
						// ctrl.apCommunity = initApCommunity; done in initLayoutConfig
						ctrl.apCommunity.taskId = response.data.task.taskId || apCommunityInfo.taskId;
						//
						ctrl.apCommunityOld.profAccts = ctrl.apCommunityOld.profAccts || [];
						ctrl.apCommunityOld.profPastdues = ctrl.apCommunityOld.profPastdues || [];
						ctrl.apCommunityOld.profFees = ctrl.apCommunityOld.profFees || [];
						ctrl.apCommunityOld.eventDoccfgs = ctrl.apCommunityOld.eventDoccfgs || [];
						ctrl.apCommunityOld.profSetlinsts = ctrl.apCommunityOld.profSetlinsts || [];
						ctrl.apCommunityOld.apMembers = ctrl.apCommunityOld.apMembers || [];
					});
				});
			});
		};

		ctrl.updateVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [$filter('encode')(ctrl.apCommunity.buyerCode)]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.apCommunity).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.apCommunity.comments = comments;
						authService.updateVerifyReject(ctrl.apCommunity).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.apcommunity.list',
					'state' : 'cda.apCommunity.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.apcommunity.list',
					'state' : 'cda.apCommunity.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);